<?php
$this->breadcrumbs=array(
    'Permission'=>array('index'),
    'Manage',
);

$this->menu=array(
);
?>
<h1>Permission</h1>
<style type="text/css">
.treeview {
    width: 30%;
    float: left;
    overflow: auto;
    height: 400px;
}
#controllers {
    width: 30%;
    float: left;
    overflow: auto;
    height: 400px;
}
#edit_form {
    width: 30%;
    float: right;
}
#edit_form .edit-granted-actions {
    display: none;
}
.treeview li {list-style: none; cursor: pointer;}
#controllers li.active,
.treeview li span.active {font-weight: bold;}
</style>
<?php
$cs = Yii::app()->clientScript;

$script = "
jQuery('body').delegate('#edit_form #roles', 'change', function(){
    var role = jQuery(this).val();
    var url = '".$this->createUrl('edit')."';
    jQuery('#edit_form').load(url, {controller : jQuery.controller, role : role});
    return false;
});

jQuery('body').delegate('#edit_form .reset', 'click', function(){
    var role = jQuery('#edit_form #roles').val();
    var url = '".$this->createUrl('edit')."';
    jQuery('#edit_form').load(url, {controller : jQuery.controller, role : role});
    return false;
});
";
$cs->registerScript(__CLASS__.'#ChangeOrResetRole', $script);

$script = "
jQuery('body').delegate('#edit_form .save', 'click', function(){
    //save data
    var actions = [];
    var remove_actions = [];
    var input = jQuery('#edit_form input:checkbox');
    input.each(function(){
        if (jQuery(this).attr('checked'))
        {
            if (jQuery(this).attr('disabled') !== 'disabled')
            {
                actions.push(jQuery(this).val());
            }
        }
        else
        {
            remove_actions.push(jQuery(this).val());
        }
    });
    
    var role = jQuery('#edit_form #roles').val();
    var url = '".$this->createUrl('/Core/service/ajax', array('SID'=>'Admin.permission.save'))."';
    jQuery.ajax({
        async : false,
        url : url,
        data : {role : role, actions : actions, remove_actions : remove_actions},
        success: function(){
            var url = '".$this->createUrl('edit')."';
            jQuery('#edit_form').load(url, {controller : jQuery.controller});
        }
    });
    
    return false;
});
";
$cs->registerScript(__CLASS__.'#Save', $script);

//fix bug CTreeView in Yii version 1.1.7
$id = Yii::getVersion() == '1.1.7' ? 'Admin.components.CTreeView' : 'system.web.widgets.CTreeView';

$this->widget($id, array('data'=>$data,'htmlOptions'=>array('class'=>'filetree')));
echo '<div id="controllers"></div>';
echo '<div id="edit_form"></div>';
echo '<div style="clear:both;"></div>';

$script = "
jQuery('.treeview li span').unbind('click').bind('click', function(event){
    if (event.target.tagName == 'SPAN')
    {
        jQuery('.treeview li span').removeClass('active');
        var url = '".$this->createUrl('controllers')."';
        var id = jQuery(this).addClass('active').closest('li').attr('id');
        jQuery('#controllers').load(url, {id : id});
        jQuery('#edit_form').html('');
    }
    return false;
});
";
$cs->registerScript(__CLASS__.'#LoadController', $script);

$script = "
jQuery('body').delegate('#controllers li', 'click', function(event){
    jQuery('#controllers li').removeClass('active');
    jQuery(this).addClass('active');
    var url = '".$this->createUrl('edit')."';
    jQuery.controller = jQuery(this).addClass('active').attr('id');
    jQuery('#edit_form').load(url, {controller : jQuery.controller});
    
    return false;
});
";
$cs->registerScript(__CLASS__.'#UpdatePermit', $script);